package shapewar.net.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import shapewar.a.widgets.Version;
import shapewar.engine.presenter.Presenter;
import shapewar.net.io.OIS;
import shapewar.net.io.OOS;
import shapewar.net.seriz.Info;
import shapewar.net.seriz.NetConf;

public class Client {
	public boolean connected = false;
	public Thread thread;
	public Receiver receiver;
	public Sender sender;
	public Room room;
	Presenter pst;
	//ObjectOutputStream oos;
	//ObjectInputStream ois;
	OOS oos;
	OIS ois;
	public int id = -1;
	public Client(Presenter pst) {
		this.pst = pst;
		pst.ect.client = this;
		receiver = new Receiver(pst);
		sender = new Sender(pst);
		sender.client = this;
		receiver.client = this;
		room = new Room(pst);
	}
	
	public void connect(final Runnable successRun,final Runnable faultRun){
		thread = new Thread(new Runnable(){
			@Override
			public void run() {
				try {
					Socket so = new Socket();//Version.server_ip
					so.connect(new InetSocketAddress(Version.server_ip,65501),1000);
					oos = new OOS(so.getOutputStream());//new ObjectOutputStream(so.getOutputStream());
					ois = new OIS(so.getInputStream());//new ObjectInputStream(so.getInputStream());
					receiver.setStream(ois,oos);
					sender.setStream(ois,oos);
					id = ois.readObject().id;
					connected = true;
					System.out.println("GetID: " + id);
					successRun.run();
					doreceive();
				} catch (IOException e) {
					e.printStackTrace();
					if(faultRun!=null)
						faultRun.run();
					exit();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
					if(faultRun!=null)
						faultRun.run();
					exit();
				} catch (ArrayIndexOutOfBoundsException e) {
					e.printStackTrace();
					if(faultRun!=null)
						faultRun.run();
					exit();
				} catch (Exception e) {
					e.printStackTrace();
					if(faultRun!=null)
						faultRun.run();
					exit();
				}
			}
		});
		thread.start();
	}
	
	public void doreceive() {
		try {
			Info info;
			while(connected){
				info = (Info) ois.readObject();
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putSerializable("info", info);
				msg.setData(bundle);
				handler.sendMessage(msg);
			}
		} catch (IOException e) {
			e.printStackTrace();
			exit();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			exit();
		} catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
			exit();
		} catch (Exception e) {
			e.printStackTrace();
			exit();
		}
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Bundle b = msg.getData();
			Info info = (Info)b.getSerializable("info");
			try{
				receiver.receive(info);
			}catch(Exception e){
				e.printStackTrace();
			}
		};
	};
	
	public void exit() {
		Info info = new Info();
		info.cmd = NetConf.info_exit;
		info.id = id;
		if(connected)
			sender.send(info);
		connected = false;
	}
	
}
